﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;

namespace XWG.JWBST.OnlineReportImageProcess
{
    /// <summary>
    /// 在线报案 图片上传
    /// </summary>
    public class Process : IProcess
    {
        public string ProcessData(StreamReader reader, HttpContext context)
        {
            XWG.JWBST.JWBSTEntity.TbBstOnlinereport report = new JWBSTEntity.TbBstOnlinereport();
            string id = context.Request.QueryString["id"].ToString();
            string type = context.Request.QueryString["type"].ToString();
            Stream inStream = context.Request.InputStream;
            byte[] UpImage = SetStreamToBytes(inStream);

            if (type == "image")
            {
                if (XWG.JWBST.JWBSTBLL.TbBstOnlinereportBLL.InsertPicByID(UpImage, id))
                {
                    return "操作成功！";
                }
                else
                {
                    return "操作失败！";
                }

            }
            else
            {
                if (XWG.JWBST.JWBSTBLL.TbBstOnlinereportBLL.InsertSoundByID(UpImage, id))
                {
                    return "操作成功！";
                }
                else
                {
                    return "操作失败！";
                }
            }
           
        }

        public byte[] SetStreamToBytes(Stream stream)
        {
            try
            {
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                // 设置当前流的位置为流的开始 
                stream.Seek(0, SeekOrigin.Begin);
                return bytes;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
